package com.damage.supplier.web;

import com.damage.common.entity.SupplyGood;
import com.damage.common.object.BaseResp;
import com.damage.supplier.service.ISupplyGoodService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@ResponseBody
@RequestMapping("supplyGood")
public class SupplyGoodController {

    @Autowired
    ISupplyGoodService supplyGoodService;

    @PostMapping("/new")
    public BaseResp newSupplyGood(SupplyGood supplyGood){
        Integer result = supplyGoodService.newSupplyGood(supplyGood);
        if (result > 0){
            return BaseResp.ok();
        }else {
            return BaseResp.error();
        }
    }

    @GetMapping("/findAll")
    public BaseResp findAll(Integer pageNum,Integer supplierId){
        PageInfo<SupplyGood> result = supplyGoodService.findBySupplierId(pageNum,supplierId);
        if (result != null){
            return BaseResp.ok(result);
        }else{
            return BaseResp.error();
        }
    }

    @GetMapping("/findSupplyGood")
    public BaseResp findSupplyGood(Integer pageNum,Integer type,String name,Integer supplierId){
        PageInfo<SupplyGood> result = supplyGoodService.findSupplyGood(pageNum,type,name,supplierId);
        if (result != null){
            return BaseResp.ok(result);
        }else{
            return BaseResp.error();
        }
    }

    @GetMapping("/findById")
    public BaseResp findById(Integer id){
        SupplyGood supplyGood = supplyGoodService.findById(id);
        if (supplyGood != null){
            return BaseResp.ok(supplyGood);
        }else{
            return BaseResp.error();
        }
    }

    @PostMapping("/update")
    public BaseResp updateSupplyGood(SupplyGood supplyGood,Boolean isInfluential){
        Integer result = supplyGoodService.update(supplyGood,isInfluential);
        if (result > 0){
            return BaseResp.ok();
        }else{
            return BaseResp.error();
        }
    }

    @PostMapping("/delete")
    public BaseResp deleteSupplier(Integer id){
        Integer result = supplyGoodService.deleteById(id);
        if (result > 0){
            return BaseResp.ok();
        }else{
            return BaseResp.error();
        }
    }

}
